package view.rules;

import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;

public class TriggerButtonListener implements ActionListener{
	
	private JPanel popPanel;
	private JButton newWordButton;
	private JComboBox newWordCombo;
	private JButton okButton;
	private JButton cancelButton;
	private JDialog triggerButtonDialog;
	okButtonActionListener okButtonListener;

	@Override
	public void actionPerformed(ActionEvent e) {
		
		popPanel = new JPanel();
		popPanel.setLayout(new MigLayout());
		
		newWordButton = new JButton("New Word");
		newWordCombo = new JComboBox();
		newWordCombo.setPreferredSize(new Dimension(495, 20));
		okButton = new JButton("OK");
		cancelButton = new JButton("Cancel");
		
		okButtonListener = new okButtonActionListener(popPanel);
		
		triggerButtonDialog = new JDialog();
		triggerButtonDialog.setModalExclusionType(Dialog.ModalExclusionType.APPLICATION_EXCLUDE);
		triggerButtonDialog.setSize(500, 250);
		triggerButtonDialog.setModal(true);
		triggerButtonDialog.setLocationRelativeTo(null);
		
		addToPopPanel();
		
		triggerButtonDialog.add(popPanel);
		triggerButtonDialog.setResizable(false);
		
		okButton.addActionListener(okButtonListener);
		okButton.setActionCommand("OK");
		cancelButton.addActionListener(okButtonListener);
		cancelButton.setActionCommand("Cancel");
		
		okButtonListener.setDialogToClose(triggerButtonDialog);
		triggerButtonDialog.setVisible(true);
		
	}
	
	private void addToPopPanel(){
		
		popPanel.add(newWordButton, "wrap");
		popPanel.add(newWordCombo, "wrap");
		popPanel.add(okButton, "wrap");
		popPanel.add(cancelButton);
		
	}
	
	private class okButtonActionListener implements ActionListener {
        private JPanel source;
        private JDialog dialog;
        
        public okButtonActionListener(JPanel source) {
            this.source = source;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
        	
            if (e.getActionCommand().equals("OK")) {
                closeDialog();
            } else if (e.getActionCommand().equals("Cancel")) {
                closeDialog();
            }
            
        }
        
        private void closeDialog() {
            if (dialog != null) {
                dialog.dispose();
            }
        }
        
        public void setDialogToClose(JDialog dialog) {
            this.dialog = dialog;
        }
    
    }

}
